package com.schmix.dpp;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.View;

public class DigitalPortionPlateActivity extends Activity {

	private static final String TAG = "DigitalPortionPlateActivity";
	private static final int SET_PREVIEW = 0;
	private SplashScreen splash;
	private Handler handler;
	private CameraPreview preview;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		splash = new SplashScreen(DigitalPortionPlateActivity.this);
		splash.showSplash();
		
		preview = new CameraPreview(DigitalPortionPlateActivity.this);
		setContentView(preview);

		handler = new Handler() {

			public void handleMessage(Message msg) {
				switch (msg.what) {
				case SET_PREVIEW:
					// initPreview();
					SharedPreferences prefs = PreferenceManager
							.getDefaultSharedPreferences(DigitalPortionPlateActivity.this);
					if (prefs.getString("plate_type", "0").equals("0")) {
						preview.addView(new CircleView(
								DigitalPortionPlateActivity.this));
					} else {
						preview.addView(new SquareView(
								DigitalPortionPlateActivity.this));
					}
					break;
				default:
					super.handleMessage(msg);
					break;
				}
			}
		};

		setContentView(R.layout.dpp);
		new PrepareActivity().execute(new Void[0]);
	}
	
    @Override
    protected void onResume() {
        super.onResume();
        preview.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        preview.pause();
    }


	class PrepareActivity extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... arg0) {
			Message msg = new Message();
			msg.what = SET_PREVIEW;
			handler.sendMessage(msg);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			splash.removeSplash();
		}
	}

	class SquareView extends View {

		public SquareView(Context context) {
			super(context);
		}

		public void draw(Canvas canvas) {
			super.draw(canvas);
			float width = (float) getWidth();
			float height = (float) getHeight();
			float radius = height / 4;
			if (width > height) {
				radius = width / 4;
			}
			Resources resources = getResources();
			Paint paint = new Paint();
			paint.setColor(resources.getColor(R.color.veggie_color));
			canvas.drawRect(new Rect((int) radius, 0, (int) (2 * radius),
					(int) width), paint);
			paint.setColor(resources.getColor(R.color.meat_color));
			canvas.drawRect(new Rect((int) (2 * radius), 0, (int) (3 * radius),
					(int) radius), paint);
			paint.setColor(resources.getColor(R.color.grain_color));
			canvas.drawRect(new Rect((int) (2 * radius), (int) radius,
					(int) (3 * radius), (int) height), paint);
		}
	}

	class CircleView extends View {

		public CircleView(Context context) {
			super(context);
		}

		public void draw(Canvas canvas) {
			super.draw(canvas);
			float width = (float) getWidth();
			float height = (float) getHeight();
			float radius = height / 4;
			if (width > height) {
				radius = width / 4;
			}
			Resources resources = getResources();
			RectF rect = new RectF(radius, 0f, (3 * radius), height);
			Paint paint = new Paint();
			paint.setColor(resources.getColor(R.color.veggie_color));
			canvas.drawArc(rect, 90, 180, true, paint);
			paint.setColor(resources.getColor(R.color.meat_color));
			canvas.drawArc(rect, 270, 90, true, paint);
			paint.setColor(resources.getColor(R.color.grain_color));
			canvas.drawArc(rect, 0, 90, true, paint);
		}

	}
}